package com.study.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.configs.RedisKeys;
import com.study.dao.XwenMapper;
import com.study.pojo.entity.Jzg;
import com.study.pojo.resp.ResultMessage;
import com.study.service.XwenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
public class XwenServiceImpl extends ServiceImpl<XwenMapper, Jzg> implements XwenService {

    @Resource
    XwenMapper xwenMapper;

    @Autowired
    RedisTemplate redisTemplate;

    private static final Logger logger = LoggerFactory.getLogger(XwenServiceImpl.class);

    //查询所有新闻内容
    @Override
    public ResultMessage findAllNews() {
        logger.debug("进入到新闻查询");
        //判断redis中是否有该key值  是否有新闻内容
        Boolean aBoolean = redisTemplate.hasKey(RedisKeys.JZG);
        if (!aBoolean){
            //Redis中没有去查询数据库
            QueryWrapper<Jzg> newsWrapper = new QueryWrapper<>();
            List<Jzg> jzgs = xwenMapper.selectList(newsWrapper);
            //非空判断
            if (jzgs!=null&&jzgs.size()>0){
                //存储进Redis中
                redisTemplate.opsForList().leftPushAll(RedisKeys.JZG,jzgs);
                logger.debug("添加完成:"+new Date());
            }else {
                //防止内存穿透
                List list = new ArrayList<>();
                list.add("");
                redisTemplate.opsForList().leftPushAll(RedisKeys.JZG,list);
                redisTemplate.expire(RedisKeys.JZG,60, TimeUnit.SECONDS);
            }
        }
        //从redis中获取数据
        List range = redisTemplate.opsForList().range(RedisKeys.JZG, 0, -1);
        System.out.println("range = " + range);
        if (StringUtils.isEmpty(range.toString())){
            return new ResultMessage().ERROR();
        }else{
            logger.debug("获取完成:"+new Date());
            return new ResultMessage(1,"ok",range,null);
        }
    }

    //根据新闻Id获取新闻
    @Override
    public ResultMessage findNewsById(Integer newsId) {
        logger.debug("进入到根据Id查询新闻接口");
        //判断Redis中是否有该key值
        //从Redis中获取新闻内容
        List<LinkedHashMap> range = redisTemplate.opsForList().range(RedisKeys.JZG, 0, -1);
        Map o=null;
//        hutool
        for (LinkedHashMap o1 : range) {

            if (o1.get("id").equals(newsId)){
                o =  o1;
                break;
            }
        }
//        Object o = redisTemplate.opsForList().index(RedisKeys.JZG, newsId);
        //判空
        if (CollectionUtils.isEmpty(o)){
            Jzg jzg = xwenMapper.selectById(newsId);
            return new ResultMessage(1,"ok",jzg,null);
        }else{
            logger.debug("查询完成:"+new Date());
            return new ResultMessage(1,"ok",o,null);
        }
        //Jzg jzg = xwenMapper.selectById(id);
    }




}
